package com.ktg.mes.rec.service.impl;

import java.util.Arrays;
import java.util.Date;
import java.util.List;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.ktg.common.utils.SecurityUtils;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import com.ktg.mes.rec.mapper.RecDispersedRecordMapper;
import com.ktg.mes.rec.domain.RecDispersedRecord;
import com.ktg.mes.rec.service.IRecDispersedRecordService;



/**
 * 分散记录Service业务层处理
 *
 * @author byteco
 * @date 2022-10-31
 */
@Service
public class RecDispersedRecordServiceImpl extends ServiceImpl<RecDispersedRecordMapper, RecDispersedRecord> implements IRecDispersedRecordService
{
    @Autowired
    private RecDispersedRecordMapper recDispersedRecordMapper;



    /**
     * 查询分散记录
     *
     * @param entity 分散记录
     * @param page 分页对象
     * @return 分散记录
     */
    @Override
    public IPage<RecDispersedRecord> queryPageList(RecDispersedRecord entity, Page<RecDispersedRecord> page) {
        LambdaQueryWrapper<RecDispersedRecord> lqw = buildQueryWrapper(entity);
        lqw.orderByDesc(RecDispersedRecord::getCreateTime);
        IPage<RecDispersedRecord> pageList = this.page(page, lqw);
        return pageList;
    }

    /**
     * 查询分散记录
     *
     * @param entity 分散记录
     * @return 分散记录
     */
    @Override
    public List<RecDispersedRecord> queryList(RecDispersedRecord entity) {
        LambdaQueryWrapper<RecDispersedRecord> lqw = buildQueryWrapper(entity);
        lqw.orderByDesc(RecDispersedRecord::getCreateTime);
        return this.list(lqw);
    }

    public RecDispersedRecord getInfo(Long id)
    {
        QueryWrapper<RecDispersedRecord> queryWrapper = new QueryWrapper<>();
        queryWrapper.lambda().eq(RecDispersedRecord::getDispersedId, id);
        return this.getOne(queryWrapper);
    }


    /**
     * 新增分散记录
     *
     * @param entity 分散记录
     * @return 结果
     */
    @Override
    public void insert(RecDispersedRecord entity)
    {
        entity.setCreateBy(SecurityUtils.getUsername());
        entity.setCreateTime(new Date());
        this.save(entity);
    }

    /**
     * 修改分散记录
     *
     * @param entity 分散记录
     * @return 结果
     */
    @Override
    public boolean update(RecDispersedRecord entity)
    {
        entity.setUpdateTime(new Date());
        entity.setUpdateBy(SecurityUtils.getUsername());
        return this.updateById(entity);
    }

    /**
     * 批量删除分散记录
     *
     * @param dispersedIds 需要删除的分散记录主键
     * @return 结果
     */
    @Override
    public boolean delete(Long[] dispersedIds)
    {

        return this.removeByIds(Arrays.asList(dispersedIds));
    }

    /**
     * 删除分散记录信息
     *
     * @param dispersedId 分散记录主键
     * @return 结果
     */
    @Override
    public boolean delete(Long dispersedId)
    {
        return this.removeById(dispersedId);
    }

    private LambdaQueryWrapper<RecDispersedRecord> buildQueryWrapper(RecDispersedRecord entity) {
        LambdaQueryWrapper<RecDispersedRecord> lqw = Wrappers.lambdaQuery();
        lqw.eq(StringUtils.isNotBlank(entity.getWorkshop()), RecDispersedRecord::getWorkshop, entity.getWorkshop());
        lqw.like(StringUtils.isNotBlank(entity.getMachineName()), RecDispersedRecord::getMachineName, entity.getMachineName());
        lqw.like(StringUtils.isNotBlank(entity.getProcessName()), RecDispersedRecord::getProcessName, entity.getProcessName());
        lqw.eq(StringUtils.isNotBlank(entity.getToolTypeCode()), RecDispersedRecord::getToolTypeCode, entity.getToolTypeCode());
        lqw.like(StringUtils.isNotBlank(entity.getToolTypeName()), RecDispersedRecord::getToolTypeName, entity.getToolTypeName());
        lqw.eq(StringUtils.isNotBlank(entity.getSpecification()), RecDispersedRecord::getSpecification, entity.getSpecification());
        lqw.like(StringUtils.isNotBlank(entity.getTaskName()), RecDispersedRecord::getTaskName, entity.getTaskName());
        lqw.eq(StringUtils.isNotBlank(entity.getSpeed()), RecDispersedRecord::getSpeed, entity.getSpeed());
        lqw.eq(entity.getStartTime() != null, RecDispersedRecord::getStartTime, entity.getStartTime());
        lqw.eq(StringUtils.isNotBlank(entity.getDuration()), RecDispersedRecord::getDuration, entity.getDuration());
        return lqw;
    }
}
